<?php

function getchungloai($id,&$sub =''){
    $sql  = "SELECT * FROM danhmuc WHERE  parent_id = ".intval($id)." AND hien_thi = 1";
    $query = mysql_query($sql); 
    while ( $row = mysql_fetch_array($query) ){
            if( $row['parent_id'] == 0){
                $sub .= "<option  disabled='disabled' style ='color:red;'>  ".$row['tendm']." </option>";
            }else{
                $sub .= "<option value='".$row['id']."' > &nbsp&nbsp<sup>".$row['tendm']." </sup></option>";
            }
             getchungloai($row['id'],$sub);
    }
    
    
   return $sub; 
}
//convert money
function convert_money($number)
{
$money = number_format($number);
$money = preg_replace('/,/i', '.', $money);
return $money;
}
//Đếm số lượt truy cập
function counttruycap(){
    $countfile = "demtruycap.abc";
    $cf = fopen($countfile,"r");
    $count = fread($cf , filesize($countfile));
    fclose($cf);
    $count++;
     
     $cf = fopen($countfile,"w");
     fwrite($cf,$count);
     fclose($cf);
     return $count;
}
// lấy ảnh đầu tiên hiển thị trong mục tin tức
function get_first_img_tintuc($idtt){
    $sql_tintuc = "SELECT * FROM tintuc WHERE id = ".$idtt;
    $qr_tintuc = mysql_query($sql_tintuc) or die("không select được id tin".mysql_error());
    $resul_tintuc = mysql_fetch_array($qr_tintuc);
    $noidung = $resul_tintuc['noidung'];
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',$noidung, $matches);
    $first_img = $matches [1] [0];
    if(empty($first_img)){ //Defines a default image
    $first_img = "images/default_tin.jpg";
}
return $first_img;
    
}

// cắt chữ
function dots_string($text,$num){
    
        if(mb_strlen($text,'UTF-8') > $num){

            $str = mb_strpos($text,' ',$num,'UTF-8');
            $dots = '...';
            if(!$str){
                $str = mb_strlen($text,'UTF-8');
                $dots = '';
            }

        }else{
            $str = mb_strlen($text,'UTF-8');
            $dots ='';
        }                   
        return mb_substr($text,0,$str,'UTF-8').$dots;
    }

 ?>